package com.fanhao.dllo.soundfm.fragment.Infofragmnet;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader;
import com.fanhao.dllo.soundfm.R;
import com.fanhao.dllo.soundfm.activity.PlayActivity;
import com.fanhao.dllo.soundfm.adapter.AnchorInforAdapter;
import com.fanhao.dllo.soundfm.entity.AnchorInforBottomResult;
import com.fanhao.dllo.soundfm.entity.AnchorInforTopResult;
import com.fanhao.dllo.soundfm.fragment.BaseFragment;
import com.fanhao.dllo.soundfm.tools.MyOnclickListener;
import com.fanhao.dllo.soundfm.utils.CountToOthers;
import com.fanhao.dllo.soundfm.volley.GetBitmap;
import com.fanhao.dllo.soundfm.volley.VolleySingleton;
import com.google.gson.Gson;

import java.util.List;

/**
 * Created by dllo on 16/1/25.
 */
public class AnchorInforFragment extends BaseFragment implements MyOnclickListener, View.OnClickListener {
    private RecyclerViewHeader headerAnchor;
    private RecyclerView anchorInforRv;
    private AnchorInforAdapter anchorAdapter;
    private ImageView backGroundIv, anchorIv, backIv;
    private TextView nameAnchotTv, descripAnchorTv, lookAnchorTv, fansAnchorTv, likesAnchorTv, soundAnchorTv;
    private long uid;

    public AnchorInforFragment(long uid) {
        this.uid = uid;
    }

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_anchor_infor;
    }

    @Override
    protected void initView() {
        backIv = findView(R.id.ivBack_anchorInfor);
        backGroundIv = findView(R.id.ivBackGround_anchorInfor);
        anchorIv = findView(R.id.ivAnchor_anchorInfor);
        nameAnchotTv = findView(R.id.tvName_anchorInfor);
        descripAnchorTv = findView(R.id.tvDescrip_anchorInfor);
        lookAnchorTv = findView(R.id.tvLook_anchorInfor);
        fansAnchorTv = findView(R.id.tvFans_anchorInfor);
        likesAnchorTv = findView(R.id.tvLikes_anchorInfor);
        soundAnchorTv = findView(R.id.tvSound_anchorInfor);
        backIv.setOnClickListener(this);

        headerAnchor = findView(R.id.recyclerViewHeader_anchorInfor);
        anchorInforRv = findView(R.id.recyclerView_anchor);
        anchorInforRv.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        anchorAdapter = new AnchorInforAdapter(getActivity());
        anchorInforRv.setAdapter(anchorAdapter);
        headerAnchor.attachTo(anchorInforRv, true);
        anchorAdapter.setMyOnclickListener(this);

        initOtherNetWork();
    }

    // 声音RecyclerView的解析
    private void initOtherNetWork() {
        String url = "http://mobile.ximalaya.com/mobile/others/ca/track/" + uid + "/1/30?position=1&device=android&title=%E6%83%85%E6%84%9F%E7%94%9F%E6%B4%BB";
        Log.d("AnchorInforFragment", url);
        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        AnchorInforBottomResult bottomResult = gson.fromJson(response,
                                AnchorInforBottomResult.class);
                        if (bottomResult != null) {
                            List<AnchorInforBottomResult.ListEntity> listEntity = bottomResult.getList();
                            if (listEntity != null && listEntity.size() > 0) {
                                anchorAdapter.addBottomData(listEntity);
                            }
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        VolleySingleton.getInstance().addRequest(request);
    }

    @Override
    protected void initData() {
        String url = "http://mobile.ximalaya.com/mobile/others/ca/homePage?position=1&toUid=" + uid + "&device=android&title=%E6%83%85%E6%84%9F%E7%94%9F%E6%B4%BB";
        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        AnchorInforTopResult topResult = gson.fromJson(response,
                                AnchorInforTopResult.class);
                        if (topResult != null) {
                            nameAnchotTv.setText(topResult.getNickname());
                            descripAnchorTv.setText(topResult.getPersonalSignature());
                            lookAnchorTv.setText(topResult.getFollowings() + "");
                            fansAnchorTv.setText(
                                    CountToOthers.getCountToWan(topResult.getFollowers()));
                            likesAnchorTv.setText(topResult.getFavorites() + "");
                            soundAnchorTv.setText("主播声音 ("+topResult.getTracks() + ")");
                            GetBitmap.getBitmap(topResult.getBackgroundLogo(), backGroundIv);
                            GetBitmap.getBitmap(topResult.getMobileMiddleLogo(),anchorIv);
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        VolleySingleton.getInstance().addRequest(request);
    }

    @Override
    public void myListener(long uid, int pos) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), PlayActivity.class);
        Bundle bundle = new Bundle();
        bundle.putLong("trackId", uid);
        intent.putExtras(bundle);
        getActivity().startActivity(intent);
    }

    @Override
    public void myNoListener(String content,int p) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivBack_anchorInfor:
                goBackToRight();
                break;
        }
    }
}
